package com.learn.springboot3.intercept;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Enumeration;

/**
 * ClassName: MyHandleInterceptor
 * Package: com.learn.springboot3.intercept
 * Description:
 *
 * @Author jiangCheng
 * @Create 2024/10/24 15:39
 * @Version 1.0
 */
@Component
@Slf4j
public class MyHandleInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        int remotePort = request.getRemotePort();
        String name = request.getParameter("name");


        log.info("port:"+remotePort+"name:"+name);

        if (name.equals("jiangcheng")){
            return false;
        }
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            log.info("headerName:"+headerName+"headerValue:"+headerValue);
        }

        response.getWriter().write("拦截器拦截了");



        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
